﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Infragistics.Win.UltraWinTree;
using QLNHK.BLL;
using Microsoft.VisualBasic.ApplicationServices;
using System.Threading;
using QLNHK.QUANTRIHETHONG;
using QLNHK.DANHMUC;
using System.Collections;
using Infragistics.Win.UltraWinToolbars;

namespace QLNHK
{
    public partial class frmMain : FrmBase
    {
        public static Cls_NGUOIDUNG ObjNguoiDung = new Cls_NGUOIDUNG();
        public static Cls_DS_CHUCNANG ObjChucNang = new Cls_DS_CHUCNANG();
        public static bool IsLoginSuccess;
        ArrayList ArrayChucNangDuocDung = new ArrayList();
        Cls_NHOM_NGUOIDUNG_CHITIET ObjNhomNguoiDung = new Cls_NHOM_NGUOIDUNG_CHITIET();
        Cls_PHANQUYEN_NHOM ObjPhanQuyenNhom = new Cls_PHANQUYEN_NHOM();
        Cls_NHOM_NGUOIDUNG_CHITIET[] arrNhomNguoiDung;
        Cls_PHANQUYEN_NHOM[] arrQuyenNhom;
        cls_Role ObjRole;
        frmWait frmWaiting;

        public frmMain()
        {
            InitializeComponent();
        }
        private void frmMain_Load(object sender, EventArgs e)
        {
            this.Hide();

            //Check bản quyền
            SKGL.Validate objValidate = new SKGL.Validate();
            objValidate.secretPhase = cHardware.GetMotherBoardID();
            objValidate.Key = ConfigNHK.Install.MaKichHoat;

            if (objValidate.IsValid) //Key hợp lệ
            {
                if (objValidate.IsExpired)
                {

                    MessageBox.Show("Phần mềm đã hết hạn sử dụng, vui lòng liên hệ nhà cung cấp.", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    frmKichHoatSanPham f = new frmKichHoatSanPham();
                    f.ShowDialog();
                    return;
                }
                // int a = objValidate.DaysLeft;
                this.Text = "Hệ thống quản lý nhân khẩu - đối tượng (Đã được kích hoạt) - Được sử dụng đến: " + objValidate.ExpireDate.ToString("dd/MM/yyyy") + " - Số ngày còn lại: " + objValidate.DaysLeft.ToString();
            }
            else
            {
                MessageBox.Show("Phần mềm chưa được kích hoạt, vui lòng liên hệ nhà cung cấp để được kích hoạt.", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                frmKichHoatSanPham f = new frmKichHoatSanPham();
                f.ShowDialog();
                return;

            }
            frmDangNhap frm = new frmDangNhap();
            if (frm.ShowDialog(this) == DialogResult.OK)
            {
                frmWaiting = new frmWait();
                ThreadPool.QueueUserWorkItem(OnProcess);
                frmWaiting.SetCaption("Vui lòng chờ giây lát");
                frmWaiting.ShowDialog(this);

                stbInfo.Panels["User"].Text = "Tài khoản: " + ObjNguoiDung.Ten_NguoiDung;
                stbInfo.Panels["LIC"].Text = "Tình trạng: Đã đăng ký sử dụng - Thời hạn sử dụng đến: " + objValidate.ExpireDate.ToString("dd/MM/yyyy");
            }
            else
                Application.Exit();
        }
        private void OnProcess(object o)
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new MethodInvoker(delegate
                {
                    frmWaiting.SetCaption("Hệ thống đang nạp dữ liệu các chức năng");

                    //Load phân quyền tại đây

                    #region TẠO CẤU TRÚC CÂY PHÂN QUYỀN MENU
                    bool createNewTreeSecurity = Convert.ToBoolean(Properties.Settings.Default.CreateNewTreeSecurity);
                    if (createNewTreeSecurity == true)
                    {
                        int level = 1;

                        //TAB
                        RibbonTab tab = null;
                        RibbonGroup group = null;
                        ToolBase item = null;
                        ToolBase itemChild = null;
                        ToolBase itemChild_lev2 = null;

                        for (int i = 0; i < ultraToolbarsManager1.Ribbon.NonInheritedRibbonTabs.Count; i++)
                        {
                            tab = null;
                            tab = ultraToolbarsManager1.Ribbon.NonInheritedRibbonTabs[i];
                            if (ObjChucNang.IsExists(tab.Key) == false)
                                ObjChucNang.Insert("frmMain", tab.Key, tab.Caption, level);
                            else
                                ObjChucNang.UpdateByTen("frmMain", tab.Key, tab.Caption, level);

                            //GROUP
                            for (int j = 0; j < tab.Groups.Count; j++)
                            {
                                group = null;
                                group = tab.Groups[j];
                                if (ObjChucNang.IsExists(group.Key) == false)
                                    ObjChucNang.Insert(tab.Key, group.Key, group.Caption, level + 1);
                                else
                                    ObjChucNang.UpdateByTen(tab.Key, group.Key, group.Caption, level + 1);

                                //ITEM
                                for (int k = 0; k < group.Tools.Count; k++)
                                {
                                    item = null;
                                    item = group.Tools[k];
                                    if (ObjChucNang.IsExists(item.Key) == false)
                                        ObjChucNang.Insert(group.Key, item.Key, item.CaptionResolved, level + 2);
                                    else
                                        ObjChucNang.UpdateByTen(group.Key, item.Key, item.CaptionResolved, level + 2);

                                    //PopupMenuTool level 1
                                    if (item.GetType().Name == "PopupMenuTool")
                                    {
                                        for (int l = 0; l < ((PopupMenuTool)item).Tools.Count; l++)
                                        {
                                            itemChild = null;
                                            itemChild = ((PopupMenuTool)item).Tools[l];
                                            if (ObjChucNang.IsExists(itemChild.Key) == false)
                                                ObjChucNang.Insert(item.Key, itemChild.Key, itemChild.CaptionResolved, level + 3);
                                            else
                                                ObjChucNang.UpdateByTen(item.Key, itemChild.Key, itemChild.CaptionResolved, level + 3);

                                            //PopupMenuTool level 2
                                            if (itemChild.GetType().Name == "PopupMenuTool")
                                            {
                                                for (int m = 0; m < ((PopupMenuTool)itemChild).Tools.Count; m++)
                                                {
                                                    itemChild_lev2 = null;
                                                    itemChild_lev2 = ((PopupMenuTool)itemChild).Tools[m];
                                                    if (ObjChucNang.IsExists(itemChild_lev2.Key) == false)
                                                        ObjChucNang.Insert(itemChild.Key, itemChild_lev2.Key, itemChild_lev2.CaptionResolved, level + 4);
                                                    else
                                                        ObjChucNang.UpdateByTen(itemChild.Key, itemChild_lev2.Key, itemChild_lev2.CaptionResolved, level + 4);
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                    #endregion

                    #region CHECK ROLE
                    if (IsLoginSuccess)
                    {
                        if (!ObjNguoiDung.IsAdministrator)
                        {
                            lockAllChucNang(true, ultraToolbarsManager1, null); //Reset menu
                            ArrayChucNangDuocDung.Clear();// Reset
                            //lấy danh sách phân quyền 
                            arrNhomNguoiDung = ObjNhomNguoiDung.GetGroupByUserID(ObjNguoiDung.ID_NguoiDung);
                            if (arrNhomNguoiDung.Length > 0)
                            {
                                string strChucNang;
                                for (int i = 0; i < arrNhomNguoiDung.Length; i++)
                                {
                                    arrQuyenNhom = ObjPhanQuyenNhom.GetRoleByGroupID(arrNhomNguoiDung[i].ID_Nhom_NguoiDung);
                                    for (int j = 0; j < arrQuyenNhom.Length; j++)
                                    {
                                        strChucNang = ObjChucNang.ListByID_DS_ChucNang(arrQuyenNhom[j].ID_DS_ChucNang).Ten;
                                        if (!ArrayChucNangDuocDung.Contains(strChucNang))
                                        {
                                            ArrayChucNangDuocDung.Add(strChucNang);
                                        }
                                    }
                                }
                                this.unlockChucNangForNguoiDung(ultraToolbarsManager1);
                            }
                        }
                        else
                        {
                            this.unlockChucNangForNguoiDung(ultraToolbarsManager1);
                        }
                    }
                    #endregion

                    frmWaiting.SetCaption("Hệ thống đang tiến hành nạp dữ liệu cấu trúc địa bàn");
                    this.builtTreeDiaBan(ref frmWaiting);
                    frmWaiting.DialogResult = DialogResult.OK;
                    Show();
                }));

            }
        }

        private void ultraToolbarsManager1_ToolClick(object sender, Infragistics.Win.UltraWinToolbars.ToolClickEventArgs e)
        {
            this.CallForm(e.Tool.Key);
        }
        private void CallForm(string menuKey)
        {
            try
            {
                bool isShowFormDialog = false;
                foreach (Form mdichild in this.MdiChildren)
                {
                    if (mdichild.Tag.ToString() == menuKey)
                    {
                        mdichild.Activate();
                        return;
                    }
                }

                Form frm = null;

                switch (menuKey)
                {
                    #region Menu Hệ thống

                    //Menu Hệ thống
                    case "MnuNguoiDung":    // ButtonTool
                        frm = new FrmDanhSachNguoiDung();
                        frm.Tag = "MnuNguoiDung";
                        break;
                    case "MnuNhomNguoiDung":    // ButtonTool
                        frm = new FrmDanhSachNhomNguoiDung();
                        frm.Tag = "MnuNhomNguoiDung";
                        break;
                    case "MnuPhanQuyenChoNhom":    // ButtonTool
                        frm = new FrmPhanQuyenNhomNguoiDung();
                        frm.Tag = "MnuPhanQuyenChoNhom";
                        break;
                    case "mnuCauHinhTienIch":    // ButtonTool
                        frm = new frmCauHinhTienIch();
                        frm.Tag = "mnuCauHinhTienIch";
                        isShowFormDialog = true;
                        break;
                    case "mnuSaoLuuPhucHoiDuLieu":
                        frm = new FrmBackupRestore();
                        frm.Tag = "mnuSaoLuuPhucHoiDuLieu";
                        break;
                   // case "MnuThoat":    // ButtonTool
                    case "mnuThoat":    // ButtonTool
                        Application.Exit();
                        break;

                    #endregion

                    #region Menu Danh mục

                    //Menu Ho khau - Nhan khau
                    case "mnuDMQuocTich":
                        frm = new FrmDM(DMCatalogue.DM_QUOCTICH, false);
                        frm.Tag = DMCatalogue.DM_QUOCTICH;
                        break;
                    case "mnuDMDanToc":
                        frm = new FrmDM(DMCatalogue.DM_DANTOC, false);
                        frm.Tag = DMCatalogue.DM_DANTOC;
                        break;
                    case "mnuDMTonGiao":
                        frm = new FrmDM(DMCatalogue.DM_TONGIAO, false);
                        frm.Tag = DMCatalogue.DM_TONGIAO;
                        break;
                    case "mnuDMQuanHeGiaDinh":
                        frm = new FrmDM(DMCatalogue.DM_QUANHE_GIADINH, false);
                        frm.Tag = DMCatalogue.DM_QUANHE_GIADINH;
                        break;

                    case "mnuDMTrinhDoHocVan":
                        frm = new FrmDM(DMCatalogue.DM_TRINHDO_HOCVAN, false);
                        frm.Tag = DMCatalogue.DM_TRINHDO_HOCVAN;
                        break;
                    case "mnuDMTrinhDoChuyenMon":
                        frm = new FrmDM(DMCatalogue.DM_TRINHDO_CHUYENMON, false);
                        frm.Tag = DMCatalogue.DM_TRINHDO_CHUYENMON;
                        break;
                    case "mnuDMTrinhDoNgoaiNgu":
                        frm = new FrmDM(DMCatalogue.DM_TRINHDO_NGOAINGU, false);
                        frm.Tag = DMCatalogue.DM_TRINHDO_NGOAINGU;
                        break;

                    case "mnuDMLoaiHo":
                        frm = new FrmDM(DMCatalogue.DM_LOAIHOKHAU, false);
                        frm.Tag = DMCatalogue.DM_LOAIHOKHAU;
                        break;

                    //Danh muc Doi tuong
                    case "mnuDMPhanLoaiDoiTuong":
                        frm = new FrmDM(DMCatalogue.DM_LOAIDOITUONG, false);
                        frm.Tag = DMCatalogue.DM_LOAIDOITUONG;
                        break;
                    case "mnuDMDanhMucDoiTuong":
                        frm = new FrmDM(DMCatalogue.DM_DANHMUCDOITUONG, false);
                        frm.Tag = DMCatalogue.DM_DANHMUCDOITUONG;
                        break;
                    case "mnuDMHeDoiTuong":
                        frm = new FrmDM(DMCatalogue.DM_HEDOITUONG, false);
                        frm.Tag = DMCatalogue.DM_HEDOITUONG;
                        break;

                    //Dia ban
                    case "mnuDMThanhPho":
                        frm = new frmDMThanhPho();
                        frm.Tag = "mnuDMThanhPho";
                        break;

                    case "mnuDMQuanHuyen":
                        frm = new frmDMQuanHuyen();
                        frm.Tag = "mnuDMQuanHuyen";
                        break;

                    case "mnuDMPhuongXa":
                        frm = new frmDMXaPhuong();
                        frm.Tag = "mnuDMPhuongXa";
                        break;

                    case "mnuDMToDanPho":
                        frm = new frmDMToDanPho();
                        frm.Tag = "mnuDMToDanPho";
                        break;
                    #endregion

                    #region Ho khau - Nhan khau
                    case "mnuQuanLyHoKhau":    // ButtonTool
                        frm = new frmQuanLyHoKhau();
                        frm.Tag = "mnuQuanLyHoKhau";
                        break;
                    case "mnuThemMoiHoKhau":
                        frm = new frmThemMoiHoKhau();
                        frm.Tag = "mnuThemMoiHoKhau";
                        isShowFormDialog = true;
                        break;

                    case "mnuQuanLyNhanKhau":
                        frm = new FrmQuanLyNhanKhau();
                        frm.Tag = "mnuQuanLyNhanKhau";
                        break;
                    case "mnuThemMoiNhanKhau":
                        frm = new frmThemMoiNhanKhau();
                        frm.Tag = "mnuThemMoiNhanKhau";
                        isShowFormDialog = true;
                        break;
                    #endregion

                    #region Doi tuong
                    case "mnuQuanLyDoiTuong":    // ButtonTool
                        frm = new FrmQuanLyDoiTuong();
                        frm.Tag = "mnuQuanLyDoiTuong";
                        break;
                    case "mnuThemMoiDoiTuong":
                        frm = new FrmThemMoiDoiTuong();
                        frm.Tag = "mnuThemMoiDoiTuong";
                        isShowFormDialog = true;
                        break;
                    #endregion

                    #region Báo cáo
                    case "mnuDanhSachNhanKhauTheoGioiTinh":
                        frm = new frmBaoCaoDanhSachNhanKhauTheoGioiTinh();
                        frm.Tag = "mnuDanhSachNhanKhauTheoGioiTinh";
                        break;
                    case "mnuDanhSachNhanKhauTheoDoTuoi":
                        frm = new frmBaoCaoDanhSachNhanKhauTheoDoTuoi();
                        frm.Tag = "mnuDanhSachNhanKhauTheoDoTuoi";
                        break;
                    case "mnuThongKeTongSoNhanKhauTheoPhuongQuan":
                        frm = new frmBaoCaoTongSoNhanKhauTheoPhuongQuan();
                        frm.Tag = "mnuThongKeTongSoNhanKhauTheoPhuongQuan";
                        break;
                    case "mnuDanhSachNguonNVQS":
                        frm = new frmBaoCaoDanhSachNguonNVQS();
                        frm.Tag = "mnuDanhSachNguonNVQS";
                        break;
                    case "mnuDanhSachNhanKhauCMND":
                        frm = new frmBaoCaoDanhSachNhanKhauCMND();
                        frm.Tag = "mnuDanhSachNhanKhauCMND";
                        break;
                    case "mnuThongKeTongSoHoKhau":
                        frm = new frmBaoCaoTongSoHoKhau();
                        frm.Tag = "mnuThongKeTongSoHoKhau";
                        break;
                    case "mnuDanhSachNhanKhauTheoDanTocTonGiao":
                        frm = new frmBaoCaoDanhSachNhanKhauTheoDanTocTonGiao();
                        frm.Tag = "mnuDanhSachNhanKhauTheoDanTocTonGiao";
                        break;
                    case "mnuBaoCaoNhanKhauChuyenDiNoiKhac":
                        frm = new frmBaoCaoDanhSachNhanKhauChuyenDiNoiKhac();
                        frm.Tag = "mnuBaoCaoNhanKhauChuyenDiNoiKhac";
                        break;
                    case "mnuBaoCaoTongSoNhanKhauChuyenDen":
                        frm = new frmBaoCaoTongSoNhanKhauChuyenDen();
                        frm.Tag = "mnuBaoCaoTongSoNhanKhauChuyenDen";
                        break;
                    case "mnuBaoCaoTongHop":
                        frm = new frmGridBaoCaoTongHop();
                        frm.Tag = "mnuBaoCaoTongHop";
                        break;
                    #endregion

                    case "mnuHuongDanSuDung":
                        System.Diagnostics.Process.Start(Application.StartupPath + "\\HDSD.pdf");
                        isShowFormDialog = true;
                        break;
                }

                frm.Tag = menuKey;
                if (!isShowFormDialog)
                {
                    frm.MdiParent = this;
                    frm.Show();
                }
                else
                {
                    if (menuKey != "mnuHuongDanSuDung")
                    {
                        isShowFormDialog = false;
                        frm.ShowDialog();
                    }
                }

            }
            catch (Exception ex) { Log(ex); }

        }

        private void unlockChucNangForNguoiDung(Infragistics.Win.UltraWinToolbars.UltraToolbarsManager mainMenu)
        {
            try
            {
                Cursor = Cursors.WaitCursor;

                //mainMenu.Office2007UICompatibility = false;
                Hashtable htTab = new Hashtable();
                Hashtable htGroup = new Hashtable();
                Hashtable htPopupMenu = new Hashtable();
                Hashtable htPopupMenu_Child = new Hashtable();

                //TAB
                RibbonTab tab = null;
                RibbonGroup group = null;
                ToolBase item = null;
                ToolBase itemChild = null;
                ToolBase itemChild_lev2 = null;

                for (int i = 0; i < mainMenu.Ribbon.NonInheritedRibbonTabs.Count; i++)
                {
                    tab = null;
                    tab = mainMenu.Ribbon.NonInheritedRibbonTabs[i];

                    //Tabs
                    if (!htTab.Contains(tab.Key))
                        htTab.Add(tab.Key, 0);
                    else
                        htTab[tab.Key] = 0;

                    //GROUP
                    for (int j = 0; j < tab.Groups.Count; j++)
                    {
                        group = null;
                        group = tab.Groups[j];

                        //Groups
                        if (!htGroup.Contains(group.Key))
                            htGroup.Add(group.Key, 0);
                        else
                            htGroup[group.Key] = 0;

                        //ITEM
                        for (int k = 0; k < group.Tools.Count; k++)
                        {
                            item = null;
                            item = group.Tools[k];

                            //PopupMenuTool level 1
                            if (item.GetType().Name == "PopupMenuTool")
                            {
                                //Group PopupMenu 1
                                if (!htPopupMenu.Contains(item.Key))
                                    htPopupMenu.Add(item.Key, 0);
                                else
                                    htPopupMenu[item.Key] = 0;

                                for (int l = 0; l < ((PopupMenuTool)item).Tools.Count; l++)
                                {
                                    itemChild = null;
                                    itemChild = ((PopupMenuTool)item).Tools[l];

                                    //PopupMenuTool level 2
                                    if (itemChild.GetType().Name == "PopupMenuTool")
                                    {
                                        //Group PopupMenu 2
                                        if (!htPopupMenu_Child.Contains(itemChild.Key))
                                            htPopupMenu_Child.Add(itemChild.Key, 0);
                                        else
                                            htPopupMenu_Child[itemChild.Key] = 0;

                                        for (int m = 0; m < ((PopupMenuTool)itemChild).Tools.Count; m++)
                                        {
                                            itemChild_lev2 = null;
                                            itemChild_lev2 = ((PopupMenuTool)itemChild).Tools[m];

                                            unlockChucNangInGroupItem(htPopupMenu_Child, itemChild, itemChild_lev2);
                                        }

                                        itemChild.SharedProps.Visible = itemChild.SharedProps.Enabled = (int.Parse(htPopupMenu_Child[itemChild.Key].ToString()) > 0);

                                        if (htPopupMenu.Contains(item.Key))
                                            htPopupMenu[item.Key] = int.Parse(htPopupMenu[item.Key].ToString()) + (itemChild.Key != "MnuThoat" && itemChild.SharedProps.Visible ? 1 : 0);
                                    }
                                    else
                                    {
                                        unlockChucNangInGroupItem(htPopupMenu, item, itemChild);
                                    }
                                }

                                item.SharedProps.Visible = item.SharedProps.Enabled = (int.Parse(htPopupMenu[item.Key].ToString()) > 0);

                                if (htGroup.Contains(group.Key))
                                    htGroup[group.Key] = int.Parse(htGroup[group.Key].ToString()) + (item.Key != "MnuThoat" && item.SharedProps.Visible ? 1 : 0);
                            }
                            else
                            {
                                unlockChucNangInGroup(htGroup, group, item);
                            }

                            if ((item.Key == "mnuHeThong") || (item.Key == "mnuDangNhap") || (item.Key == "MnuThoat") || (item.Key == "MnuDangXuat") || (item.Key == "mnuKhoaUngDung")
                                || (item.Key == "mnuPhienBanDuLieu") || (item.Key == "MnuCauHinhKetNoi"))
                            {
                                item.SharedProps.Visible = true;
                                item.SharedProps.Enabled = true;
                            }
                        }

                        group.Visible = (int.Parse(htGroup[group.Key].ToString()) > 0);

                        if (htTab.Contains(tab.Key))
                            htTab[tab.Key] = int.Parse(htTab[tab.Key].ToString()) + (group.Visible ? 1 : 0);
                    }

                    tab.Visible = (int.Parse(htTab[tab.Key].ToString()) > 0);
                }
            }
            catch (Exception ex)
            {
                Logger.LocalLogger.Instance().WriteMessage(ex);
            }
            finally { Cursor = Cursors.Default; }
        }
        private void unlockChucNangInGroupItem(Hashtable htPopupMenu, ToolBase item, ToolBase itemChild)
        {
            if (ObjNguoiDung.IsAdministrator)
            {
                itemChild.SharedProps.Enabled = true;
                itemChild.SharedProps.Visible = true;
            }
            else
            {
                if (ArrayChucNangDuocDung.Contains(itemChild.Key))
                {
                    ObjRole = new cls_Role(Global.GetRole(ObjPhanQuyenNhom.GetRoleByIDChucNang(frmMain.ObjNguoiDung.ID_NguoiDung, itemChild.Key)));

                    itemChild.SharedProps.Visible = ObjRole.KichHoat;
                    itemChild.SharedProps.Enabled = true;
                }
                else
                    itemChild.SharedProps.Visible = false;
            }

            if (htPopupMenu.Contains(item.Key))
                htPopupMenu[item.Key] = int.Parse(htPopupMenu[item.Key].ToString()) + (itemChild.Key != "MnuThoat" && itemChild.SharedProps.Visible ? 1 : 0);
        }
        private void unlockChucNangInGroup(Hashtable htGroup, RibbonGroup group, ToolBase item)
        {
            if (ObjNguoiDung.IsAdministrator)
            {
                item.SharedProps.Enabled = true;
                item.SharedProps.Visible = true;
            }
            else
            {
                if (ArrayChucNangDuocDung.Contains(item.Key))
                {
                    ObjRole = new cls_Role(Global.GetRole(ObjPhanQuyenNhom.GetRoleByIDChucNang(frmMain.ObjNguoiDung.ID_NguoiDung, item.Key)));

                    item.SharedProps.Visible = ObjRole.KichHoat;
                    item.SharedProps.Enabled = true;
                }
                else
                    item.SharedProps.Visible = false;
            }

            if (htGroup.Contains(group.Key))
                htGroup[group.Key] = int.Parse(htGroup[group.Key].ToString()) + (item.Key != "MnuThoat" && item.SharedProps.Visible ? 1 : 0);
        }
        /// <summary>
        /// Khóa các chức năng của hệ thống
        /// </summary>
        /// <param name="isLock"></param>
        /// <param name="mainMenu"></param>
        /// <param name="mnuItem"></param>
        private void lockAllChucNang(bool isLock, Infragistics.Win.UltraWinToolbars.UltraToolbarsManager mainMenu, Infragistics.Win.UltraWinToolbars.ToolBase mnuItem)
        {
            try
            {
                Cursor = Cursors.WaitCursor;

                //mainMenu.Office2007UICompatibility = false;
                RibbonTab tab = null;
                RibbonGroup group = null;
                ToolBase item = null;
                ToolBase itemChild = null;

                for (int i = 0; i < mainMenu.Ribbon.NonInheritedRibbonTabs.Count; i++)
                {
                    tab = null;
                    tab = mainMenu.Ribbon.NonInheritedRibbonTabs[i];

                    for (int j = 0; j < tab.Groups.Count; j++)
                    {
                        group = null;
                        group = tab.Groups[j];

                        for (int k = 0; k < group.Tools.Count; k++)
                        {
                            item = null;
                            item = group.Tools[k];

                            if (item.GetType().Name == "PopupMenuTool")
                            {
                                for (int l = 0; l < ((PopupMenuTool)item).Tools.Count; l++)
                                {
                                    itemChild = null;
                                    itemChild = ((PopupMenuTool)item).Tools[l];

                                    ObjRole = new cls_Role(Global.GetRole(ObjPhanQuyenNhom.GetRoleByIDChucNang(frmMain.ObjNguoiDung.ID_NguoiDung, itemChild.Key)));
                                    if (!ObjRole.KichHoat)
                                    {
                                        itemChild.SharedProps.Visible = ObjRole.KichHoat;
                                    }
                                    else
                                    {
                                        if ((itemChild.Key != "mnuHeThong") && (itemChild.Key != "mnuDangNhap") && (itemChild.Key != "MnuThoat") && (itemChild.Key != "MnuDangXuat") && (itemChild.Key != "mnuKhoaUngDung") && (itemChild.Key != "mnuPhienBanDuLieu") && (itemChild.Key != "MnuCauHinhKetNoi"))
                                            itemChild.SharedProps.Enabled = !isLock;
                                    }
                                }
                            }
                            else
                            {
                                ObjRole = new cls_Role(Global.GetRole(ObjPhanQuyenNhom.GetRoleByIDChucNang(frmMain.ObjNguoiDung.ID_NguoiDung, item.Key)));
                                if (!ObjRole.KichHoat)
                                {
                                    item.SharedProps.Visible = ObjRole.KichHoat;
                                }
                                else
                                {
                                    if ((item.Key != "mnuHeThong") && (item.Key != "mnuDangNhap") && (item.Key != "MnuThoat") && (item.Key != "MnuDangXuat") && (item.Key != "mnuKhoaUngDung") && (item.Key != "mnuPhienBanDuLieu") && (item.Key != "MnuCauHinhKetNoi"))
                                        item.SharedProps.Enabled = !isLock;
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.LocalLogger.Instance().WriteMessage(ex);
            }
            finally { Cursor = Cursors.Default; }
        }
        private void lockAllMidiForm(bool isLock)
        {
            foreach (Form mdichild in this.MdiChildren)
            {
                mdichild.Enabled = !isLock;
            }
        }

        public void builtTreeDiaBan(ref frmWait frmW)
        {
            trvDiaBan.ResetNodes();

            string capsudung = "QUANHUYEN";
            if (ConfigNHK.Install.CapSuDung != null)
                capsudung = ConfigNHK.Install.CapSuDung;

            if (capsudung == "QUANHUYEN")
            {
                #region QUANHUYEN
                UltraTreeNode NodeThanhPho;
                UltraTreeNode NodeQuanHuyen;
                UltraTreeNode NodeXaPhuong;

                string queryThanhPho = "1=1";
                if (ConfigNHK.Install.ThanhPho > 0)
                    queryThanhPho += " AND ID = " + ConfigNHK.Install.ThanhPho;

                foreach (DataRow drThanhPho in ThanhPho.SelectDynamic(queryThanhPho, "Ten ASC").Tables[0].Rows)
                {
                    NodeThanhPho = trvDiaBan.Nodes.Add();
                    NodeThanhPho.Text = drThanhPho["Loai"].ToString() + " " + drThanhPho["Ten"].ToString();
                    frmW.SetCaption("Đang tạo cấu trúc " + NodeThanhPho.Text);
                    NodeThanhPho.Key = "TP-" + drThanhPho["ID"].ToString();
                    NodeThanhPho.Expanded = true;
                    string queryQuanHuyen = "IDThanhPho=" + drThanhPho["ID"].ToString();
                    if (ConfigNHK.Install.QuanHuyen > 0)
                        queryQuanHuyen += " AND ID=" + ConfigNHK.Install.QuanHuyen;

                    foreach (DataRow drQuanHuyen in QuanHuyen.SelectDynamic(queryQuanHuyen, "Ten ASC").Tables[0].Rows)
                    //foreach (DataRow drQuanHuyen in QuanHuyen.SelectDynamic("IDThanhPho=" + drThanhPho["ID"].ToString(), "Ten ASC").Tables[0].Rows)
                    {
                        NodeQuanHuyen = NodeThanhPho.Nodes.Add();
                        NodeQuanHuyen.Text = drQuanHuyen["Loai"].ToString() + " " + drQuanHuyen["Ten"].ToString();
                        NodeQuanHuyen.Key = "QH-" + drQuanHuyen["ID"].ToString();

                        foreach (DataRow drXaPhuong in XaPhuong.SelectDynamic("IDQuanHuyen=" + drQuanHuyen["ID"].ToString(), "Ten ASC").Tables[0].Rows)
                        {
                            NodeXaPhuong = NodeQuanHuyen.Nodes.Add();
                            NodeXaPhuong.Text = drXaPhuong["Loai"].ToString() + " " + drXaPhuong["Ten"].ToString();
                            NodeXaPhuong.Key = "XP-" + drXaPhuong["ID"].ToString();
                        }
                    }
                }
                #endregion
            }
            if (capsudung == "XAPHUONG")
            {
                #region XAPHUONG

                UltraTreeNode NodeThanhPho;
                UltraTreeNode NodeQuanHuyen;
                UltraTreeNode NodeXaPhuong;
                UltraTreeNode NodeToDanPho;

                string queryQuanHuyen = "IDThanhPho=" + ConfigNHK.Install.ThanhPho.ToString();
                if (ConfigNHK.Install.QuanHuyen > 0)
                    queryQuanHuyen += " AND ID=" + ConfigNHK.Install.QuanHuyen;

                foreach (DataRow drQuanHuyen in QuanHuyen.SelectDynamic(queryQuanHuyen, "Ten ASC").Tables[0].Rows)
                {
                    NodeQuanHuyen = trvDiaBan.Nodes.Add();
                    NodeQuanHuyen.Text = drQuanHuyen["Loai"].ToString() + " " + drQuanHuyen["Ten"].ToString();
                    frmW.SetCaption("Đang tạo cấu trúc " + NodeQuanHuyen.Text);
                    NodeQuanHuyen.Key = "QH-" + drQuanHuyen["ID"].ToString();
                    NodeQuanHuyen.Expanded = true;

                    string queryXaPhuong = "IDQuanHuyen=" + ConfigNHK.Install.QuanHuyen.ToString();
                    if (ConfigNHK.Install.QuanHuyen > 0)
                        queryXaPhuong += " AND ID=" + ConfigNHK.Install.XaPhuong;

                    foreach (DataRow drXaPhuong in XaPhuong.SelectDynamic(queryXaPhuong, "Ten ASC").Tables[0].Rows)
                    {
                        NodeXaPhuong = NodeQuanHuyen.Nodes.Add();
                        NodeXaPhuong.Text = drXaPhuong["Loai"].ToString() + " " + drXaPhuong["Ten"].ToString();
                        NodeXaPhuong.Key = "XP-" + drXaPhuong["ID"].ToString();

                        foreach (DataRow drToDanPho in ToDanPho.SelectDynamic("IDPhuongXa=" + drXaPhuong["ID"].ToString(), "Ten ASC").Tables[0].Rows)
                        {
                            NodeToDanPho = NodeXaPhuong.Nodes.Add();
                            NodeToDanPho.Text = drToDanPho["Loai"].ToString() + " " + drToDanPho["Ten"].ToString();
                            NodeToDanPho.Key = "TDP-" + drToDanPho["ID"].ToString();
                        }
                    }
                }

                #endregion
            }





        }
        private void trvDiaBan_AfterSelect(object sender, SelectEventArgs e)
        {
            frmBase_Rebind frmActive;
            //Kiểm tra form active hiện tại có thừa kế từ form frmBase_Rebind
            // OK : gọi phương thức Rebinding để raise event
            //Xác định Form đang được kích hoạt
            if (this.ActiveMdiChild != null && this.ActiveMdiChild.GetType().IsSubclassOf(typeof(frmBase_Rebind)))
            {
                //Lay thong dia ban dang chon
                if (trvDiaBan.ActiveNode != null)
                {
                    string capsudung = "QUANHUYEN";
                    if (ConfigNHK.Install.CapSuDung.Trim() != "")
                        capsudung = ConfigNHK.Install.CapSuDung;

                    if (capsudung == "QUANHUYEN")
                    {
                        if (trvDiaBan.ActiveNode.Key.Contains("TP-"))
                        {
                            BLL.Global.CACHE_THANHPHO_ID = System.Convert.ToInt32(trvDiaBan.ActiveNode.Key.Replace("TP-", ""));
                            BLL.Global.CACHE_QUANHUYEN_ID = 0;
                            BLL.Global.CACHE_XAPHUONG_ID = 0;
                            BLL.Global.CACHE_TODANPHO_ID = 0;
                        }
                        else if (trvDiaBan.ActiveNode.Key.Contains("QH-"))
                        {
                            BLL.Global.CACHE_THANHPHO_ID = System.Convert.ToInt32(trvDiaBan.ActiveNode.Parent.Key.Replace("TP-", ""));
                            BLL.Global.CACHE_QUANHUYEN_ID = System.Convert.ToInt32(trvDiaBan.ActiveNode.Key.Replace("QH-", ""));
                            BLL.Global.CACHE_XAPHUONG_ID = 0;
                            BLL.Global.CACHE_TODANPHO_ID = 0;
                        }
                        else if (trvDiaBan.ActiveNode.Key.Contains("XP-"))
                        {
                            BLL.Global.CACHE_THANHPHO_ID = System.Convert.ToInt32(trvDiaBan.ActiveNode.Parent.Parent.Key.Replace("TP-", ""));
                            BLL.Global.CACHE_QUANHUYEN_ID = System.Convert.ToInt32(trvDiaBan.ActiveNode.Parent.Key.Replace("QH-", ""));
                            BLL.Global.CACHE_XAPHUONG_ID = System.Convert.ToInt32(trvDiaBan.ActiveNode.Key.Replace("XP-", ""));
                            BLL.Global.CACHE_TODANPHO_ID = 0;
                        }
                    }
                    if (capsudung == "XAPHUONG")
                    {
                        BLL.Global.CACHE_THANHPHO_ID = System.Convert.ToInt32(ConfigNHK.Install.ThanhPho);

                        if (trvDiaBan.ActiveNode.Key.Contains("QH-"))
                        {
                            BLL.Global.CACHE_QUANHUYEN_ID = System.Convert.ToInt32(trvDiaBan.ActiveNode.Key.Replace("QH-", ""));
                            BLL.Global.CACHE_XAPHUONG_ID = 0;
                            BLL.Global.CACHE_TODANPHO_ID = 0;
                        }
                        else if (trvDiaBan.ActiveNode.Key.Contains("XP-"))
                        {
                            BLL.Global.CACHE_QUANHUYEN_ID = System.Convert.ToInt32(trvDiaBan.ActiveNode.Parent.Key.Replace("QH-", ""));
                            BLL.Global.CACHE_XAPHUONG_ID = System.Convert.ToInt32(trvDiaBan.ActiveNode.Key.Replace("XP-", ""));
                            BLL.Global.CACHE_TODANPHO_ID = 0;
                        }
                        else if (trvDiaBan.ActiveNode.Key.Contains("TDP-"))
                        {
                            BLL.Global.CACHE_QUANHUYEN_ID = System.Convert.ToInt32(trvDiaBan.ActiveNode.Parent.Parent.Key.Replace("QH-", ""));
                            BLL.Global.CACHE_XAPHUONG_ID = System.Convert.ToInt32(trvDiaBan.ActiveNode.Parent.Key.Replace("XP-", ""));
                            BLL.Global.CACHE_TODANPHO_ID = System.Convert.ToInt32(trvDiaBan.ActiveNode.Key.Replace("TDP-", ""));
                        }
                    }


                }

                frmActive = (frmBase_Rebind)this.ActiveMdiChild;
                frmActive.Rebinding();
            }
        }
    }
}
